import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:transparent_image/transparent_image.dart';
import 'package:cached_network_image/cached_network_image.dart';

class Counter extends StatefulWidget{
  const Counter({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _CounterState();

}

class _CounterState extends State{

  int _counter = 0;

  //生成一个列表
  final items =  List<ListItem>.generate(
    1200,
        (i) => i % 6 == 0
        ?  HeadingItem("Heading $i")
        :  MessageItem("Sender $i", "Message body $i"),
  );
  // final items = List<String>.generate(10000, (i) => "Item $i");

  void _increment() {
    setState(() {
      // This call to setState tells the Flutter framework that
      // something has changed in this State, which causes it to rerun
      // the build method below so that the display can reflect the
      // updated values. If we changed _counter without calling
      // setState(), then the build method would not be called again,
      // and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {

   return MaterialApp(
      title: 'c Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue
      ),
      home: Column(
        children: <Widget>[
          RaisedButton(
            onPressed: _increment,
            child: const Text('Increment'),
          ),
          Text('Count: $_counter'),
          Image.network("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF",height: 100,width: 100,),
          FadeInImage.memoryNetwork(placeholder: kTransparentImage, image: "https://t7.baidu.com/it/u=91673060,7145840&fm=193&f=GIF",width: 100,height: 100,),
          CachedNetworkImage(imageUrl:"https://t7.baidu.com/it/u=91673060,7145840&fm=193&f=GIF",width: 100, height: 100,progressIndicatorBuilder:(context, url,progress) =>
              CircularProgressIndicator( value: progress.progress,),
          ),
         // Expanded(child: SizedBox(height:300,child:  ListView(scrollDirection:Axis.horizontal,children:const [
         //  Text("测试1",style:TextStyle(color: Colors.blue),),
         //  Text("测试2"),
         //  Text("测试3"),
         // ]),)),
         //  Expanded(child: SizedBox(height:300,child:
         //    ListView.builder(itemCount:items.length ,itemBuilder:(context,index){
         //      return Text(items[index]);
         //    },
         //   )
         //  ),
         //   SizedBox(height: 300,child: ListView.builder(itemCount: items.length,itemBuilder: (context,index) {
         //           var item = items[index];
         //             if(item is MessageItem){
         //               return Text("我是MessageItem");
         //             }else if(item is HeadingItem){
         //               return Text("HeadingItem");
         //             }else{
         //               return Text("我是空数据");
         //             }
         //   },),
         //   )
         ],
      )
    );
  }

}

//测试显示不同的条目类型
//1、生成一个基类

abstract class ListItem{}


class HeadingItem extends ListItem{

  final String heading;

  HeadingItem(this.heading);
}

class MessageItem extends ListItem{
    final String sender;
    final String body;

    MessageItem(this.sender,this.body);

}






//显示和更改分离

class CounterDisplay  extends StatelessWidget{

   CounterDisplay({this.count});

  final int? count;

  @override
  Widget build(BuildContext context) {
    return Text('Count: $count');
  }
}

class CounterIncrementor extends StatelessWidget {
   CounterIncrementor({required this.onPressed});
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) {
    return  RaisedButton(
      onPressed: onPressed,
      child:  const Text('Increment'),
    );
  }
}

class Counterx extends StatefulWidget {
  @override
  _CounterStatex createState() => _CounterStatex();
}

class _CounterStatex extends State<Counter> {
  int _counter = 0;

  void _increment() {
    setState(() {
      ++_counter;
    });
  }

  @override
  Widget build(BuildContext context) {
    return  Row(children: <Widget>[
       CounterIncrementor(onPressed: _increment),
       CounterDisplay(count: _counter),
    ]);
  }
}







